package org.yuanzheng.kafka

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import org.apache.kafka.common.serialization.StringSerializer

import scala.util.Random

/**
 * @author yuanzheng
 * @date 2020/6/11-22:09
 */
object KafkaProducer {
  def main(args: Array[String]): Unit = {
    // 连接Kafka数据源
    val props = new Properties()
    props.setProperty("bootstrap.servers", "192.168.1.8:9092")
    props.setProperty("key.serializer", classOf[StringSerializer].getName)
    props.setProperty("value.serializer", classOf[StringSerializer].getName)
    val producer = new KafkaProducer[String, String](props)
    val r = new Random()
    while (true) {
      val data = new ProducerRecord[String, String]("yuan", "key" + r.nextInt(10), "value" + r.nextDouble())
      producer.send(data)
      Thread.sleep(1000)
    }
  }
}
